﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using QuickWeb.Resources;
using System.Windows.Media;
using System.Windows.Input;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;


namespace QuickWeb
{
    public partial class MainPage : PhoneApplicationPage
    {
        private bool resultPresentQVar;
        public bool resultPresentQ
        {
            get { return resultPresentQVar; }
            set { resultPresentQVar = value; }
        }
        

        // Constructor
        public MainPage()
        {
            
            InitializeComponent();
            
            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            browser.Visibility = Visibility.Collapsed;
            browser.IsScriptEnabled = true;
            browser.Navigating += new EventHandler<NavigatingEventArgs>(browser_Navigating);
            resultPresentQ = false;

            Visibility v = (Visibility)Resources["PhoneLightThemeVisibility"];
            string iconString = "";

            //iconString = "/Assets/overflowdots_dark.png";
            //Image im = new Image();
            //BitmapImage myBitmapImage = new BitmapImage(new Uri(iconString, UriKind.Relative));
            //myBitmapImage.DecodePixelHeight = 47;
            //myBitmapImage.DecodePixelWidth = 117;

            //im.Source = myBitmapImage;
            //settingsButton.Content = im;
            BuildLocalizedApplicationBar();
            ThemeManager.ToDarkTheme();
        }

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        private void Pivot_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void searchBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (searchBox.Text == AppResources.Search )
            {
                searchBox.Text = "";
                SolidColorBrush Brush1 = new SolidColorBrush();
                Brush1.Color = Colors.Black;
                searchBox.Foreground = Brush1;
            }
        }

        private void searchBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (searchBox.Text == String.Empty)
            {
                searchBox.Text = AppResources.Search;
                SolidColorBrush Brush2 = new SolidColorBrush();
                Brush2.Color = Colors.Gray;
                searchBox.Foreground = Brush2;
            }
        }

        private void searchBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                browser.Visibility = Visibility.Visible;
                progressBar.IsIndeterminate = true;
                resultPresentQ = false;
                browser.Source = new Uri("https://duckduckgo.com/?q="+searchBox.Text, UriKind.Absolute);
                browser.Focus();
            }
        }

        void browser_Navigating(object sender, NavigatingEventArgs e) {
            if (resultPresentQ)
            {
                var task = new WebBrowserTask();
                task.Uri = e.Uri;
                task.Show();
                e.Cancel = true;
            }
            else {
                e.Cancel = false;
            }            
        }

        private void browser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            progressBar.IsIndeterminate = false;
            resultPresentQ = true;
        }

        private void settingsTriggered(object sender, EventArgs e)
        {
            

        }


        // Build a localized ApplicationBar
        private void BuildLocalizedApplicationBar()
        {
            // Set the page's ApplicationBar to a new instance of ApplicationBar.
            ApplicationBar = new ApplicationBar();

            // Create a new button and set the text value to the localized string from AppResources.
            Visibility v = (Visibility)Resources["PhoneLightThemeVisibility"];
            string iconString = "";
            iconString = "/Assets/settings_dark.png";
            
            ApplicationBarIconButton appBarButton =
                new ApplicationBarIconButton(new
                Uri(iconString, UriKind.Relative));
            appBarButton.Text = AppResources.settings;
            ApplicationBar.Buttons.Add(appBarButton);
            appBarButton.Click += new EventHandler(settingsTriggered);

            ApplicationBar.Mode = ApplicationBarMode.Minimized;
            // Create a new menu item with the localized string from AppResources.
            //ApplicationBarMenuItem appBarMenuItem =
            //    new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
            //ApplicationBar.MenuItems.Add(appBarMenuItem);
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (pivot.SelectedIndex == 0)
            //{
            //    ApplicationBar.IsVisible = true;
            //}
            //else {
            //    ApplicationBar.IsVisible = false;
            //}
        }


    }
}